package org.itboys.portal.web.controller.mobile;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.commons.utils.tools.ResponseManager;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.portal.dto.mobile.MobiPortalInfoResult;
import org.itboys.portal.dto.mobile.MobiPortalItemResult;
import org.itboys.portal.entity.PortalInfo;
import org.itboys.portal.service.PortalInfoService;
import org.itboys.portal.service.manager.MobiPortalInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/** 
 * @author 作者 huml 
 * @version 创建时间：2016-3-30 上午9:45:16 
 * 手机端资讯接口 
 */
@RestController
@RequestMapping("/mobile/core/portal/info")
public class CoreMobiPortalInfoController extends BaseController {

	@Autowired
	private MobiPortalInfoManager portalInfoManager;
	@Autowired
	private PortalInfoService portalInfoService;
	@Autowired
	private ResourceHolder resourceHolder;
	/**
	 * 资讯栏目列表
	 * @param response
	 */
	@RequestMapping(value="/itemList", method=RequestMethod.POST)
	public void itemList(HttpServletResponse response){
		try {
			List<MobiPortalItemResult> results = portalInfoManager.itemList();
			if(results == null){
				AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getMsgCodeResultDO(1, "暂无资讯栏目"));
			} else {
				AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getMsgResultDO(results, CommonConstants.SUCCESS));
			}
		} catch (Exception e) {
			logger.error("/mobile/core/portal/info/itemList error", e);
			ResponseManager.SystemError(response, null);
		}
	}
	/**
	 * 根据资讯栏目信息获取资讯id
	 * @param request
	 * @param itemId  资讯栏目id
	 * @param response
	 */
	@RequestMapping(value="/infoList", method=RequestMethod.POST)
	public void infoList(HttpServletRequest request,
			@RequestParam("itemId")Long itemId,
			HttpServletResponse response){
		try {
			List<MobiPortalInfoResult> results = portalInfoManager.getInfosByItemId(itemId, request);
			AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getMsgResultDO(results, CommonConstants.SUCCESS));
		} catch (Exception e) {
			logger.error("/mobile/core/portal/info/infoList error", e);
			ResponseManager.SystemError(response, null);
		}
	}
	
	/**
	 * 根据资讯栏目信息获取资讯id
	 * @param request
	 * @param itemId  资讯栏目id
	 * @param response
	 */
	@RequestMapping(value="/infoListByItemCode", method=RequestMethod.POST)
	public void infoList(HttpServletRequest request,
			@RequestParam("code")String code,
			HttpServletResponse response){
		try {
			List<MobiPortalInfoResult> results = portalInfoManager.getInfosByItemCode(code, request);
			AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getMsgResultDO(results, CommonConstants.SUCCESS));
		} catch (Exception e) {
			logger.error("/mobile/core/portal/info/infoListByItemCode error", e);
			ResponseManager.SystemError(response, null);
		}
	}
	
	/**
	 * 信息详情网页版
	 * @param id
	 * @param type 这里用于判断是不是分享出去的
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/html")
	public ModelAndView html(@RequestParam("id")Long id , 
			Model model){
		PortalInfo info = portalInfoService.getById(id);
		if(StringUtils.isNoneEmpty(info.getImage())){
			info.setImage(resourceHolder.getStringValue("imgRoot")+info.getImage());
		}
		if(info.getCreateTime() != null){
			info.setTime(TimeUtils.format(info.getCreateTime(), CommonConstants.DATE.FORMAT_YYYY_MM_dd));
		}
		model.addAttribute("info", info);
		return new ModelAndView("portal/info/empty");
	}
	
}
